qinglan.Canvas.State = qinglan.Canvas.Class(function(id, fsm){
	this.id = id;
	this.fsm = fsm;

	this.listener = new qinglan.Listener('state_'+id, this);

	this.isSuspended = false;	
	this.lastState = -1;
	this.init();
}).methods({
	init: function(){
		qinglan.Array.each(qinglan.Canvas.State.addAdviceList, function(value) {
				var self = this;
				this[value] = function(name, fun) {
					return qinglan.Canvas.State._addAdvice.call(self,value.toLowerCase().slice(2), name, fun);
				};
			},this);
		// 删除事件
		qinglan.Array.each(qinglan.Canvas.State.removeAdvice, function(value) {
				var self = this;
				this[value] = function(name) {
					return qinglan.Canvas.State._removeAdvice.call(self,value.toLowerCase().slice(2), name);
				};
			}, this);
	},
	enter: function(msg, fromState){ 
		this.listener.notify('enter', [msg, fromState]);
	},
	leave: function(){
		this.listener.notify('leave');
	},
	message: function(msg){
		this.listener.notify('message',[msg]);
	},
	suspend: function(){ 
		this.listener.notify('suspend');
	},
	resume: function(msg, fromState){ 
		this.listener.notify('resume', [msg, fromState]);
	},
	update: function(){
		this.listener.notify('update');
	},
	isSuspend: function(){
		return this.isSuspended;
	},
	setFsm: function(fsm){
		this.fsm = fsm;
	},
	addAdvice: function(type, name, fun){
		this.listener.addAdvice(type, name, fun);
	},
	removeAdvice: function(type, name){
		this.listener.removeAdvice(type, name);
	}
}).statics({
	addAdviceList: ['onEnter', 'onLeave', 'onMessage', 'onSuspend', 'onResume', 'onUpdate'],
	removeAdvice: ['reEnter', 'reLeave', 'reMessage', 'reSuspend', 'reResume', 'reUpdate'],
	_addAdvice: function(){
		(this instanceof qinglan.Canvas.State) && (this.listener.addAdvice(type, name, fun));
	},
	_removeAdvice: function(value,name){
		(this instanceof qinglan.Canvas.State) && (this.listener.removeAdvice(type, name));
	}
});

